<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>菜单之键盘导航 - jQuery EasyUI 演示</title>
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../../themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../demo.css">
	<script type="text/javascript" src="../../jquery.min.js"></script>
	<script type="text/javascript" src="../../jquery.easyui.min.js"></script>
</head>
<body>
	<h2 title="Keyboard Navigation in Menu">菜单之键盘导航</h2>
	<p title="Press Alt+W to focus the menu. Once the menu get focus, you will be able to navigate menu using keyboard keys.">　　摁下【Alt+W】键可让菜单获得焦点。一旦菜单获得焦点，即可用键盘的各键来进行导航操作。对于有子项的菜单项，键盘方向箭头键可导航，主要用于PC；移动设备不太适宜，非要用的话还是长按2秒（最好避开文字）。</p>
	<div style="margin:20px 0;"></div>
	
	<div class="easyui-panel" title="&nbsp;菜单" style="width:150px;">
		<div id="mm" data-options="inline:true" style="width:100%">
			<div onclick="javascript:alert('new')">新建</div>
			<div>
				<span>打开</span>
				<div style="width:150px;">
					<div><b>Word</b></div>
					<div>Excel</div>
					<div>PowerPoint</div>
					<div>
						<span>菜单1</span>
						<div style="width:120px;">
							<div>子项1</div>
							<div>子项2</div>
							<div>
								<span>子项…</span>
								<div style="width:80px;">
									<div onclick="javascript:alert('sub21')">子项21</div>
									<div>子项22</div>
									<div>子项23</div>
								</div>
							</div>
							<div>子项3</div>
						</div>
					</div>
					<div>
						<span title="Window Demos">窗口演示</span>
						<div style="width:120px;">
							<div title="Window">窗口</div>
							<div title="Dialog">对话框</div>
							<div>EasyUI 官网</div>
						</div>
					</div>
				</div>
			</div>
			<div data-options="iconCls:'icon-save'">保存</div>
			<div data-options="iconCls:'icon-print',disabled:true">打印</div>
			<div class="menu-sep"></div>
			<div>退出</div>
		</div>
	</div>
	<script type="text/javascript">
		(function($){
			function getParentMenu(rootMenu, menu){
				return findParent(rootMenu);

				function findParent(pmenu){
					var p = undefined;
					$(pmenu).find('.menu-item').each(function(){
						if (!p && this.submenu){
							if ($(this.submenu)[0] == $(menu)[0]){
								p = pmenu;
							} else {
								p = findParent(this.submenu);
							}
						}
					});
					return p;
				}
			}
			function getParentItem(pmenu, menu){
				var item = undefined;
				$(pmenu).find('.menu-item').each(function(){
					if ($(this.submenu)[0] == $(menu)[0]){
						item = $(this);
						return false;
					}
				});
				return item;
			}

			$.extend($.fn.menu.methods, {
				enableNav: function(jq, rootMenu){
					var firstItemSelector = '.menu-item:not(.menu-item-disabled):first';
					var lastItemSelector = '.menu-item:not(.menu-item-disabled):last';
					return jq.each(function(){
						var menu = $(this);
						rootMenu = $(rootMenu).length ? $(rootMenu) : menu;
						menu.attr('tabindex','0').css('outline','none').unbind('.menunav').bind('keydown.menunav', function(e){
							var item = $(this).find('.menu-active');
							switch(e.keyCode){
								case 13:  // enter
									item.trigger('click');
									break;
								case 27:  // esc
									rootMenu.find('.menu-active').trigger('mouseleave');
									break;
								case 38:  // up
									var prev = item.length ? item.prevAll(firstItemSelector) : menu.find(lastItemSelector);
									prev.trigger('mouseenter');
									return false;
								case 40:  // down
									var next = item.length ? item.nextAll(firstItemSelector) : menu.find(firstItemSelector);
									next.trigger('mouseenter');
									return false;
								case 37:  // left
									var pmenu = getParentMenu(rootMenu, menu);
									if (pmenu){
										item.trigger('mouseleave');
										var pitem = getParentItem(pmenu, menu);
										if (pitem){
											pitem.trigger('mouseenter');
										}
										pmenu.focus();
									}
									return false;
								case 39:  // right
									if (item.length && item[0].submenu){
										$(item[0].submenu).menu('enableNav', rootMenu).find(firstItemSelector).trigger('mouseenter');
										$(item[0].submenu).focus();
									}
									return false;
							}
						});
					});
				}
			});
		})(jQuery);

		$(function(){
			$('#mm').menu().menu('enableNav');
			$(document).keydown(function(e){
				if (e.altKey && e.keyCode == 87){
					$('#mm').focus();
				}
			})
		})
	</script>
</body>
</html>